package com.jokins.admin.vue.user.action;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jokins.admin.vue.user.actioncontext.SysUserLoginDoc;
import com.jokins.admin.vue.user.mapper.SysUserMapper;
import com.jokins.admin.vue.user.param.SysUserLoginParam;
import com.jokins.admin.vue.user.utils.FixedHashPasswordUtils;
import com.jokins.admin.vue.user.utils.SysUserStatus;
import com.jokins.admin.vue.user.vo.SysUserLoginVo;
import com.jokins.common.actions.action.ActionContext;
import com.jokins.common.actions.action.BaseAction;
import com.jokins.common.actions.exception.BusinessException;
import com.jokins.common.entry.vue.user.SysUser;
import com.jokins.common.util.BusinessCode;
import com.jokins.common.util.SqlTypeConverter;
import com.jokins.common.util.date.DateFormatter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SysUserLoginDocAction extends BaseAction<SysUserLoginDoc> {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 登录接口
     *
     * @param actionContext
     * @throws BusinessException
     */
    @Override
    protected void process(SysUserLoginDoc actionContext) throws BusinessException {
        SysUserLoginParam param = actionContext.getParam();
        SysUserLoginVo sysUserLoginVo = new SysUserLoginVo();
        try {
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.eq("user_name", param.getUserName());
            SysUser user = sysUserMapper.selectOne(wrapper);
            if (user == null) {
                throw new BusinessException("用户不存在", BusinessCode.PARAM_ERROR);
            }
            if (user.getStatus() == SysUserStatus.PENDING_APPROVAL.getValue()) {
                throw new BusinessException("用户被禁用", BusinessCode.LOGIN_ERROR);
            }
            //TODO 稍后写
            boolean isPasswordCorrect = FixedHashPasswordUtils.compareHashedPasswords(user.getSalt(), user.getSalt());
            if (!isPasswordCorrect) {
                throw new BusinessException("密码错误", BusinessCode.PARAM_ERROR);
            } else {
                log.info("密码正确:{}", JSON.toJSONString(user));
                SqlTypeConverter.convertEntity(user, sysUserLoginVo, DateFormatter.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
                log.info("sysUserLoginVo:{}", JSON.toJSONString(sysUserLoginVo));
                UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("user_name", param.getUserName());
                updateWrapper.set("login_count", user.getLoginCount() + 1);
                sysUserMapper.update(null, updateWrapper);
                actionContext.setResult(ActionContext.RESULT_DOC_KEY, sysUserLoginVo);
            }
        } catch (Exception e) {
            log.error("复制属性失败", e);
            throw new BusinessException("系统错误", BusinessCode.SYSTEM_ERROR);
        }
    }
}
